package sdk.wufun.com.wfsdk_ysdk4switchpay;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.tencent.ysdk.module.share.ShareApi;
import com.tencent.ysdk.module.share.ShareCallBack;
import com.tencent.ysdk.module.share.impl.ShareRet;
import com.wufun.sdk.IShare;
import com.wufun.sdk.ShareParams;
import com.wufun.sdk.WFCode;
import com.wufun.sdk.WFSDK;

public class YSDKShare implements IShare {
	private Bitmap cacheBitmap;
	
	public YSDKShare(Activity context){
		
		try{
	        ShareApi.getInstance().regShareCallBack(new ShareCallBack() {
	            @Override
	            public void onSuccess(ShareRet ret) {
	            	WFSDK.getInstance().onResult(WFCode.CODE_SHARE_SUCCESS, "share success");
	                Log.d("U8SDK","分享成功！  分享路径："+ret.shareType.name()+" 透传信息："+ret.extInfo);
	            }

	            @Override
	            public void onError(ShareRet ret) {
	            	WFSDK.getInstance().onResult(WFCode.CODE_SHARE_FAILED, "share failed");
	                Log.d("WFSDK","分享失败  分享路径："+ret.shareType.name()+" 错误码："+ret.retCode+" 错误信息："+ret.retMsg+" 透传信息："+ret.extInfo);
	            }

	            @Override
	            public void onCancel(ShareRet ret) {
	            	WFSDK.getInstance().onResult(WFCode.CODE_SHARE_FAILED, "share cancel");
	                Log.d("WFSDK","分享用户取消！  分享路径："+ret.shareType.name()+" 透传信息："+ret.extInfo);
	            }
	        });
			
		}catch(Exception e){
			
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean isSupportMethod(String methodName) {
		return true;
	}

	@Override
	public void share(ShareParams params) {
		ShareApi.getInstance().share(capForBitmap(), params.getTitle(), params.getContent(), "");
	}
	
	private Bitmap capForBitmap(){
		if (cacheBitmap==null){
			View view = WFSDK.getInstance().getContext().getWindow().getDecorView();
			view.setDrawingCacheEnabled(true);
			view.buildDrawingCache();
			Rect rect = new Rect();
			view.getWindowVisibleDisplayFrame(rect);
			int statusBarHeight = rect.top;
			WindowManager windowManager = WFSDK.getInstance().getContext().getWindowManager();
			DisplayMetrics outMetrics = new DisplayMetrics();
			windowManager.getDefaultDisplay().getMetrics(outMetrics);
			int width = outMetrics.widthPixels;
			int height = outMetrics.heightPixels;
			cacheBitmap= Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width,
					height - statusBarHeight);
			view.destroyDrawingCache();
			view.setDrawingCacheEnabled(false);
		}
		return cacheBitmap;
	}

}
